<?php
namespace app\astro\controller;
use app\common\lib\exception\ApiException;
use think\Controller;
class Data extends Controller
{
    public $day_chart;// 是否为日间盘
    public $planets_display;// 不显示的行星
    public $asps_display;// 相位显隐
    public $asp_orb_all;// 相位容许度
    public $planet_orb_all;// 行星容许度
    public $details1 = [];
    public $details2 = [];
    public $returnTime = '';

    /**
     * 时间信息处理
     * $utdatenow
     * $utnow
     */
    public function deal_time($timeinfo1 = [],$timeinfo2 = [])
    {
        $year1 = $timeinfo1['year'];
        $month1 = $timeinfo1['month'];
        $day1 = $timeinfo1['day'];
        $hour1 = $timeinfo1['hour'];
        $minute1 = $timeinfo1['minute'];
        $second1 = 0;
        $is_summer1 = $timeinfo1['is_summer'];
        $timezone1 = $timeinfo1['timezone'];

        checkRecodeData($year1,$month1,$day1,$hour1,$minute1,$timezone1);

        // 夏令时
        if($is_summer1) $hour1--;
        // 时区处理
        if ($timezone1 >= 0)
        {
            $whole1 = floor($timezone1);
            $fraction1 = $timezone1 - floor($timezone1);
        }
        else
        {
            $whole1 = ceil($timezone1);
            $fraction1 = $timezone1 - ceil($timezone1);
        }
        $hour1 = $hour1 - $whole1;
        $minute1= $minute1- ($fraction1 * 60);
        $date1 = mktime($hour1, $minute1, $second1, $month1, $day1, $year1);
        if(!empty($timeinfo2))
        {
            $year2 = $timeinfo2['year'];
            $month2 = $timeinfo2['month'];
            $day2 = $timeinfo2['day'];
            $hour2 = $timeinfo2['hour'];
            $minute2 = $timeinfo2['minute'];
            $second2 = 0;
            $is_summer2 = $timeinfo2['is_summer'];
            $timezone2 = $timeinfo2['timezone'];
            checkRecodeData($year2,$month2,$day2,$hour2,$minute2,$timezone2);
            // 夏令时
            if($is_summer2) $hour2--;
            // 时区处理
            if ($timezone2 >= 0)
            {
                $whole2 = floor($timezone2);
                $fraction2 = $timezone2 - floor($timezone2);
            }
            else
            {
                $whole2 = ceil($timezone2);
                $fraction2 = $timezone2 - ceil($timezone2);
            }
            $hour2 = $hour2 - $whole2;
            $minute2 = $minute2- ($fraction2 * 60);

            $date2 = mktime($hour2, $minute2, $second2, $month2, $day2, $year2);
            return [$date1,$date2];
        }
        else
        {
            return $date1;
        }
    }

    public function execData($options = [])
    {
        $astrolog_prefix = config('astrolog.prefix');

        $exec_str = $astrolog_prefix." -b{$options['utdatenow']} -ut{$options['utnow']} -p0123456789DAmHt -eswe -house{$options['longitude']},{$options['latitude']},{$options['h_sys']} -fldsa -g, -head";

        exec ($exec_str, $out);

        if(empty($out) || count($out) < 27)
        {
            unset($out);
            $exec_str = $astrolog_prefix." -b{$options['utdatenow']} -ut{$options['utnow']} -p0123456789DAmHt -eswe -house{$options['longitude']},{$options['latitude']},w -fldsa -g, -head";

            exec ($exec_str, $out);
        }

        return $out;
    }

    /**
     * @param array $data
     * @param string $default
     * @return array
     * @throws ApiException
     */
    public function getOriginalData($data = [], $default = 'p')
    {
        if(empty($data) || count($data) < 20)
        {
            \think\Log::write('行星数据获取失败，请检查','astro_error',true);
            throw new ApiException('服务异常，请稍后重试');
        }
        $planet = [];
        $speed = [];
        foreach ($data as $key => $line)
        {
            $row = explode(',',$line);
            $planet[$key] = @floatval($row[0])*1;
            $declination[$key] = isset($row[1]) ? floatval($row[1]) *1 : 0;
            $speed[$key] = isset($row[2]) ? floatval($row[2])*1 : 0;
        };

        $juno = $planet[13];// 婚神星
        $vert = $planet[30];// 宿命点度数

        $asc = $planet[27];
        $mc = $planet[28];
        $des = $asc >= 180 ? $asc - 180 : $asc + 180;
        $ic = $mc >= 180 ? $mc - 180 : $mc + 180;

        // 判断是日间盘还是夜间盘
        if ($asc > $des)
            if ($planet[0] <= $asc && $planet[0] > $des)
                $day_chart = true;
            else
                $day_chart = false;
        else
            if ($planet[0] > $asc && $planet[0] <= $des)
                $day_chart = false;
            else
                $day_chart = true;

        // 计算福点位置
        if ($day_chart == true) $fort = $asc + $planet[1] - $planet[0];
        else $fort = $asc - $planet[1] + $planet[0];
        if ($fort >= 360) $fort -= 360;
        if ($fort < 0) $fort += 360;

        $hc = [];

        for ($i = 1; $i <= 14; $i++)
        {
            $hc[$i] = $planet[14 + $i];
            unset($planet[14 + $i]);
        }

        unset($hc[13]);unset($hc[14]);
        unset($planet[29]);unset($planet[30]);

        $planet[13] = $juno;// 婚神星
        $planet[14] = $fort;// 福点
        $planet[15] = $vert;// 宿命点
        $planet[16] = $planet[12] > 180 ? $planet[12] - 180 : $planet[12] + 180;// 增加南交点
        // 加四轴 放入行星组中
        $planet[17] = $asc ;// 上升点
        $planet[18] = $ic;// 天底
        $planet[19] = $des;// 下降点
        $planet[20] = $mc;// 天顶

        $retro = $this->getRetroData($speed);

        $returnData = ['rx1'=>$retro, 'p1'=>$planet, 'hc1'=>$hc, 'speed'=>$speed];

        $this->day_chart = $day_chart;

        return $returnData;
    }
    /**
     * 获取是否逆行数据
     * @param  [type] $speed [description]
     * @return [type]        [description]
     */
    public function getRetroData($speed)
    {
        $retro = '';
        for ($i = 0; $i <= 13; $i++)
        {
            if (@$speed[$i] < 0) $retro .= "R";
            else $retro .= " ";
        }
        return $retro;
    }
    /**
     * 隐藏不显示的行星
     * @param $arr
     * @param $hidePlanet
     */
    public function array_remove(&$arr, $hidePlanet)
    {
        for ($i=0; $i <= 20; $i++)
        {
            if ($hidePlanet[$i] == 1) continue;

            unset($arr['p1'][$i]);
            unset($arr['hpos'][$i]);
            unset($arr['speed'][$i]);
        }
    }
    /**
     * [getPlanetInHouse 获取行星所落宫位]
     * @param  [type] $longitude [description]
     * @param  [type] $house     [description]
     * @return [type]            [description]
     */
    public function getPlanetInHouse($longitude,$house)
    {

        $house[13] = $house[1];

        $in_house = 12;

        for ($j=1; $j <= 12; $j++)
        {
            // 方便计算四轴
            $pl = $longitude + (1 / 36000);

            $h_now = @$house[$j];
            $h_next = @$house[$j+1];

            if ($j < 12 && $h_now > $h_next)
            {
                if (($pl >= $h_now && $pl < 360) || ($pl < $h_next && $pl >= 0))
                {
                    return $j;
                }
            }
            if ($j == 12 && ($h_now > $house[1]))
            {
                if (($pl >= $h_now && $pl < 360) || ($pl < $house[1] && $pl >= 0))
                {
                    return $j;
                }
            }
            if (($pl >= $h_now) && ($pl < $h_next && ($j < 12)))
            {
                return $j;
            }
            if (($pl >= $h_now) && ($pl < $house[1]) && ($j == 12))
            {
                return $j;
            }
        }
        return $in_house;
    }
    /**
     * 行星落入星座度数
     * 如：18°2'26''
     * @return [type] [description]
     */
    public function inSignDegFloor($deg)
    {

        $sign_id = floor($deg/30);

        $pos_in_sign = $deg - ($sign_id * 30);

        return floatval($pos_in_sign);
    }
    /**
     * 入星座度数
     * 如：18°2'26''  || 18°3′
     * @return [type] [description]
     */
    public function inSignDeg($deg,$get_sec=false)
    {
        $longitude = $deg;
        $sign_id = floor($longitude/30);
        $pos_in_sign = $longitude - ($sign_id * 30);
        $deg = floor($pos_in_sign);
        $full_min = ($pos_in_sign - $deg) * 60;
        $min = floor($full_min);
        $sec = round(($full_min - $min) * 60);
        if ($deg < 10) $deg = '    '.$deg;
        if ($min < 10) $min = '0'.$min;
        if ($sec < 10) $sec = '0'.$sec;
        if(!$get_sec)
        {
            // 四舍五入
            if($sec >= 30 ) $min++;
            if($min == 60)
            {
                $min=0;
                $deg++;
            }
            return $deg . '°'. $min ."′";
        }
        return $deg . '°'. $min ."′". $sec .'″';
    }
    /**
     * 获取
     * 行星
     * 宫头
     * 详细信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function getPlanetHouseDetail($data, $house = array(), $year = 0)
    {
        $planetHouseDetails = [];

        for ($i=0; $i <= MC; $i++)
        {
            // 不显示的行星不进行处理
            if (!isset($data['p1'][$i])) continue;

            $longitude = $data['p1'][$i];
            $in_sign = ceil($longitude/30);// 行星在星座
            if($in_sign == 0) $in_sign = 1;// 整宫制上升0°时 BUG修复
            $in_sign_deg = $this->inSignDeg($longitude);// 行星在星座的度数
            $in_sign_deg_e = $longitude - ($in_sign-1)*30;// 行星在星座的度数
            $in_house = $this->getPlanetInHouse($longitude, $house);// 行星落宫
            $speed = @$data['speed'][$i] ? $data['speed'][$i] : 0;// 行星速度

            $planetHouseDetails['planets'][$i]['id'] = $i;
            $planetHouseDetails['planets'][$i]['speed'] = $speed;
            $planetHouseDetails['planets'][$i]['deg'] = $longitude*1;
            $planetHouseDetails['planets'][$i]['in_sign_id'] = $in_sign;
//            echo json_encode(['id'=>$i,'msg'=>$longitude,'sign'=>$in_sign]);
            $planetHouseDetails['planets'][$i]['in_sign_deg'] = $in_sign_deg;
            $planetHouseDetails['planets'][$i]['in_sign_deg_e'] = $in_sign_deg_e;

            $planetHouseDetails['planets'][$i]['in_house_id'] = $in_house;
            $planetHouseDetails['planets'][$i]['ancient'] = $this->planet_in_ancient($i,$in_sign,$this->inSignDegFloor($longitude));
            $sun_in_out = $this->oriental_or_occidental_sun($i,$data['p1'][0],$longitude);// 东出西入
            $planetHouseDetails['planets'][$i]['ancient']['sun_in_out'] = $sun_in_out;
            $planetHouseDetails['planets'][$i]['ancient']['hagy'] = $this->planet_hagy($i,$in_sign,$in_house,$sun_in_out);
            $planetHouseDetails['planets'][$i]['ancient']['sun_dist'] = $this->planet_sun_dist($i,$data['p1'][0],$longitude);
            $planetHouseDetails['planets'][$i]['ancient']['like_house'] = $this->planet_in_like_house($i,$in_house);
            $planetHouseDetails['planets'][$i]['ancient']['burn_way'] = $this->planet_in_burn_way($i,$in_sign,$in_sign_deg);
            $planetHouseDetails['planets'][$i]['ancient']['antipode'] = $this->planet_antipode($i,$longitude,$data['p1']);
            $planetHouseDetails['planets'][$i]['ancient']['star'] = [];

            if($year>0)
            {
                if(!in_array($i,[0,1,2,3,4,5,6,ASC,IC,DES,MC])) continue;
                $diff = ($year *1 -2000)*0.83/60;
                // 计算恒星 每一百年移动83′ 每年0.83分
                foreach(config('astrolog.star') as $s)
                {
                    $star = $s[0] + $diff;
                    if($star >= 360) $star -= 360;
                    $light = $s[4];// 亮度

                    $da = abs($longitude-$star);
                    // 亮度小于1.4，容许度2；亮度1.4-2.1，容许度1.5    20180211 edit by Seunk
                    if(
                        ($da <= 2 && $light < 1.4 )
                        || ($da >= 358 && $da <= 360 && $light < 1.4)
                        || ($da <= 2 && $light >= 1.4 && $light < 2.1)
                        || ($da >= 358 && $da <= 360 && $light >= 1.4 && $light < 2.1)
                        || ($da <= 1 && $light >= 2.1)
                        || ($da >= 359 && $da <= 360 && $light >= 2.1)
                    )
                    {
                        $s_in_sign = ceil($star/30);// 行星在星座
                        $s_in_sign_deg = $this->inSignDeg($star);// 行星在星座的度数  不显示秒
                        $s_s = [
                            'deg'=>$star,
                            'name'=>$s[1],
                            'belong_sign'=>$s[2],
                            'planet_attr'=>$s[3],
                            'influences'=>$s[4],
                            'description'=>$s[5],
                            'auspicious'=>$s[6],// 吉凶状态  凶 平 吉 大吉
                            'in_sign_deg'=>$s_in_sign_deg,
                            'in_sign'=>$s_in_sign,
                            'con_planet_id'=>$i,
                        ];
                        if($s[1] == '壁宿二' && $i == 0)
                        {
                            $s_s['auspicious'] = '凶';
                        }
                        elseif($s[1] == '天狼星' && $i == MC)
                        {
                            $s_s['auspicious'] = '大吉';
                        }
                        elseif($s[1] == '水委一' && $i == 5)
                        {
                            $s_s['auspicious'] = '大吉';
                        }
                        elseif($s[1] == '北落师门' && ($i == 2 || $i == 3))
                        {
                            $s_s['auspicious'] = '吉';
                        }
                        elseif($s[1] == '参宿七' && ($i == 0 || $i == 1 || $i == ASC || $i == MC))
                        {
                            $s_s['auspicious'] = '大吉';
                        }
                        elseif($s[1] == '参宿四' && $i == 4)
                        {
                            $s_s['auspicious'] = '凶';
                        }
                        elseif($s[1] == '太微左垣一' && ($i == 4 || $i == 6))
                        {
                            $s_s['auspicious'] = '平';
                        }
                        elseif($s[1] == '贯索四' && ($i == 4 || $i == 8))
                        {
                            $s_s['auspicious'] = '平';
                        }
                        elseif($s[1] == '马腹一')
                        {
                            if(($i == ASC || $i == 2) && ($i == 4 || $i == 6 || $i == 8))
                            {
                                $s_s['auspicious'] = '平';
                            }
                            elseif($i == ASC || $i == 2)
                            {
                                $s_s['auspicious'] = '吉';
                            }
                            elseif($i == 4 || $i == 6 || $i == 8)
                            {
                                $s_s['auspicious'] = '凶';
                            }
                        }

                        $planetHouseDetails['planets'][$i]['ancient']['star'][] = $s_s;
                        $planetHouseDetails['star'][] = $s_s;
                    }
                }
            }
            else
            {
                $planetHouseDetails['planets'][$i]['ancient']['star'] = null;
                $planetHouseDetails['star'] = null;
            }

        }

        //循环12宫
        for ($i=1; $i <= 12; $i++)
        {
            $house_deg = @$house[$i]*1;
            if($house_deg > 360 ) $house_deg -=360;

            $in_sign = ceil($house_deg/30);
            if($in_sign == 0) $in_sign = 1;// 整宫制上升0°时 BUG修复
            $in_sign_deg = $this->inSignDeg($house_deg);

            $planetHouseDetails['houses'][$i]['deg'] = $house_deg;
            $planetHouseDetails['houses'][$i]['in_sign_id'] = $in_sign;
            $planetHouseDetails['houses'][$i]['in_sign_deg'] = $in_sign_deg;
            // 现占 // 宫主星信息
            $protect_id = config('astrolog.protect_id')[$in_sign-1];// 宫主星id
            $planetHouseDetails['houses'][$i]['protect_id'] = $protect_id;

            $planetHouseDetails['houses'][$i]['protect_insign_id'] = $planetHouseDetails['planets'][$protect_id]['in_sign_id'];
            $planetHouseDetails['houses'][$i]['protect_insign_deg'] = $planetHouseDetails['planets'][$protect_id]['in_sign_deg'];
            // 现占 // 宫主飞星信息
            $planetHouseDetails['houses'][$i]['protect_inhouse_id'] = $planetHouseDetails['planets'][$protect_id]['in_house_id'];

            // 古占 // 宫主星信息
            $protect_ancient_id = config('astrolog.protect_ancient_id')[$in_sign-1];// 宫主星id
            $planetHouseDetails['houses'][$i]['protect_ancient_id'] = $protect_ancient_id;
            $planetHouseDetails['houses'][$i]['protect_ancient_insign_id'] = $planetHouseDetails['planets'][$protect_ancient_id]['in_sign_id'];
            $planetHouseDetails['houses'][$i]['protect_ancient_insign_deg'] = $planetHouseDetails['planets'][$protect_ancient_id]['in_sign_deg'];
            // 古占 // 宫主飞星信息
            $planetHouseDetails['houses'][$i]['protect_ancient_inhouse_id'] = $planetHouseDetails['planets'][$protect_ancient_id]['in_house_id'];
            $planetHouseDetails['houses'][$i]['in_house_area_planet_info'] = [];// 落入当前宫位的行星信息
            $num = 0;
            foreach ($planetHouseDetails['planets'] as $k => $pls)
            {
                if ($this->planets_display[$k] != 1) continue;
//                if ($k != 17 && $k != IC && $k != DES && $k != 20 && $pls['in_house_id'] == $i)
                if ($k <10 && $pls['in_house_id'] == $i)
                {
                    $planetHouseDetails['houses'][$i]['in_house_area_planet_info'][]= [
                        'id'            => $k,
                        'deg'           => $planetHouseDetails['planets'][$k]['deg'],// 行星度数
                    ];
                    $num ++;
                }
            }
            $planetHouseDetails['houses'][$i]['have_planet_num'] = $num;// 计算当前宫位有几个行星

            $planetHouseDetails['houses'][$i]['house_almuten'] = $this->house_almuten($in_sign,$this->inSignDegFloor($house_deg));//宫神星
        }
        $planetHouseDetails['arabics'] = $this->getTheArabics($planetHouseDetails['planets'],$planetHouseDetails['houses'],$this->day_chart);

        return $planetHouseDetails;
    }
    /**
     * 获取行星间相位信息
     * @param array $data1
     * @param array $data2
     * @param bool $pkself
     * @param int $vs
     * @return array
     */
    public function planetPkPlanet($data1 = array(), $data2 = array(), $pkself = false,  $vs = 0)
    {
        $planetPkPlanet = [];
        $planetPkPlanet['phase'] = [];
        $planet1 = $data1['p1'];
        $planet2 = $data2['p1'];
        $speed1 = $data1['speed'];
        $speed2 = $data2['speed'];
        // 相位计算
        for ($i=0; $i <= 20 ; $i++)
        {
            if (!isset($planet1[$i])) continue;
            // 行星1 度数
            $longitude1 = $planet1[$i];
            $p_orb1 = isset($this->planet_orb_all[$i]) ? $this->planet_orb_all[$i] : 0;
            $planetPkPlanetPhase = [];
            for ($j=0; $j <= 20; $j++)
            {
                if (!isset($planet2[$j])) continue;
                // 行星2 度数
                $longitude2 = $planet2[$j];
                $q = -1;
                // 避免重复
                if ($pkself == true && ($i == $j || ($i >= 17 && $j >= 17))) continue;
                $da = abs($longitude1 - $longitude2);
                if ($da > 180) $da = 360 - $da;

                $p_orb2 = isset($this->planet_orb_all[$j]) ? $this->planet_orb_all[$j] : 0;
                $p_orb = $p_orb1 + $p_orb2;
                $orb = null;
                // 判断相位
                foreach($this->asps_display as $k => $asps)
                {
                    $asp_deg = $asps;

                    $asp_deg_orb = $this->asp_orb_all[$asp_deg];

                    if (($da <= ($asp_deg + $asp_deg_orb + $p_orb)) && ($da >= ($asp_deg - $asp_deg_orb - $p_orb)))
                    {
                        $q = $asp_deg;
                        $orb = $asp_deg_orb + $p_orb;
                    }

                }
                if ( $q >= 0 )
                {
                    // 计算出入相位----------------------------------------------
                    if($i >= ASC || $j >= ASC)
                    {
                        $in_or_out = -1;
                    }
                    else
                    {
                        $in_or_out_status = $this->phase_in_or_out($q,$longitude1,$longitude2,$speed1[$i],$speed2[$j],$vs);
                        if($in_or_out_status) $in_or_out = 1;// 入相
                        else $in_or_out = 0;// 出相
                    }
                    $planetPkPlanetPhase[$i.'-'.$j]['phase_e'] = $q;
                    $planetPkPlanetPhase[$i.'-'.$j]['phase_or_orb'] = $orb;
                    $planetPkPlanetPhase[$i.'-'.$j]['phase_orb'] = floatval(number_format(abs($da-$q),6));
                    $planetPkPlanetPhase[$i.'-'.$j]['phase_change'] = $in_or_out;
                    $planetPkPlanetPhase[$i.'-'.$j]['id'] = $j;
                    // 判断相位方向  用于区分如同向90° 和  两个方向的90°
                    if($q == 0 || $q == 180)
                    {
                        $planetPkPlanetPhase[$i.'-'.$j]['phase_type'] = 0;// 相位方向
                    }
                    else
                    {
                        if($longitude1 < $longitude2 || ($longitude2 - $longitude1) > 180)
                        {
                            $planetPkPlanetPhase[$i.'-'.$j]['phase_type'] = 1;
                        }else{
                            $planetPkPlanetPhase[$i.'-'.$j]['phase_type'] = -1;
                        }
                    }
                    $planetPkPlanet['phase'][] = [
                        'id1'=>$i,
                        'id2'=>$j,
                        'phase_deg'     =>  $q,
                        'orb'           =>  number_format(abs($da-$q),6) * 1,
                        'or_orb' => $orb,
                    ];
                }
            }
            $planetPkPlanet['planets'][$i]['phase'] = $planetPkPlanetPhase;
        }
        return $planetPkPlanet;
    }

    /**
     *  获取太阳弧数据
     * @param  [type] $p_sun [太阳度数]
     * @param array $data
     * @return array [type]        [太阳弧行星数据]
     */
    public function getTheLongitudeBySolarArc($p_sun, $data = [])
    {
        $ret = [];
        $p1 = $data['p1'];

        $solar_arc = Crunch($p_sun - $p1[0]);

        $longitude = [];

        foreach ($p1 as $i => $p)
        {
            $longitude[$i] = Crunch($p + $solar_arc);
        }

        $ret = ['rx1'=>$data['rx1'],'p1'=>$longitude,'speed'=>$data['speed']];

        return $ret;
    }
    /**
     * [组合盘中点计算]
     * @param  array  $data1 [description]
     * @param  array  $data2 [description]
     * @return [type]        [description]
     */
    public function getTheCompoisteCenter($data1 = array(), $data2 = array())
    {
        $ret = [];

        $rx1 = "";

        for ($i = 0; $i <= 12; $i++)
        {
            $rx1 .= " ";
        }

        $longitude = [];
        $speed = [];

        foreach ($data1['p1'] as $i => $d)
        {
            $longitude[$i] = ($data1['p1'][$i] + $data2['p1'][$i] ) / 2;
            $diff = abs($data1['p1'][$i] - $data2['p1'][$i] );

            if (
                $diff >= 180
                && abs($longitude[$i] - $data1['p1'][$i]) > 90
                && abs($longitude[$i] - $data2['p1'][$i]) > 90
            )
            {
                $longitude[$i] += 180;
            }

            $longitude[$i] = floatval(sprintf("%.3f", Crunch($longitude[$i])));

            $speed[$i] = ($data1['speed'][$i] + $data2['speed'][$i] ) / 2;
        }

        for ($i = 10; $i <= 12; $i++)
        {
            $hc1x[$i - 9] = $data1['hc1'][$i];
            $hc2x[$i - 9] = $data2['hc1'][$i];
        }

        for ($i = 1; $i <= 9; $i++)
        {
            $hc1x[$i + 3] = $data1['hc1'][$i];
            $hc2x[$i + 3] = $data2['hc1'][$i];
        }

        for ($i = 1; $i <= 12; $i++)
        {
            $hc3x[$i] = ($hc1x[$i] + $hc2x[$i]) / 2;
            if (abs($hc3x[$i] - $hc1x[$i]) > 90 Or abs($hc3x[$i] - $hc2x[$i]) > 90)
            {
                $hc3x[$i] = $hc3x[$i] + 180;
            }

            if ($hc3x[$i] >= 360)
            {
                $hc3x[$i] = $hc3x[$i] - 360;
            }

            if ($i >= 2)
            {
                if (abs($hc3x[$i] - $hc3x[$i - 1]) > 90 And abs($hc3x[$i] - $hc3x[$i - 1]) < 270)
                {
                    $hc3x[$i] = Crunch($hc3x[$i] + 180);
                }
            }
        }

        for ($i = 1; $i <= 9; $i++)
        {
            $hc3[$i] = sprintf("%.3f", $hc3x[$i + 3]);
        }

        for ($i = 10; $i <= 12; $i++)
        {
            $hc3[$i] = sprintf("%.3f", $hc3x[$i - 9]);
        }

        $longitude[ASC] = $hc3[1];
        $longitude[IC] = $hc3[4];
        $longitude[DES] = $hc3[7];
        $longitude[MC] = $hc3[10];

        $ret = ['rx1'=>$rx1,'p1'=>$longitude,'hc1'=>$hc3,'speed'=>$speed];

        return $ret;
    }
//    /**
//     * 获取当前时间距出生日期过了多少个月
//     * 用于小限流月
//     * @param $date1 出生日期 年-月-日
//     * @param $date2 当前时间 年-月-日
//     * @param string $tags 拆分符
//     * @return number
//     */
//    public function getMonthNum( $date1, $date2, $tags='-' )
//    {
//        // 若当前日期小于出生日期时间戳 则返回错误
//        if (strtotime ($date2) < strtotime ($date1)) $this->error('日期错误~');
//
//        $date1 = explode($tags,$date1);
//
//        $date2 = explode($tags,$date2);
//
//        $month = 0;
//        // 日期小于出生日期  月份-1
//        if ($date2[2] < $date1[2])
//        {
//            $date2[1]--;
//        }
//        if ($date2[1] < $date1[1])
//        {
//            $date2[1] += 12;
//            $date2[0]--;
//        }
//        $month = $date2[1] - $date1[1] + ($date2[0] - $date1[0]) * 12;
//
//        return $month;
//    }
//    /**
//     * 获取日返月返时间点
//     * @param $info
//     */
//    public function getTheSunMoonReturnTime($info = array(), $type = 'sun')
//    {
//        $astrologsrc = 'astrolog';
//        $astrolog = 'astrolog';
//
//        @putenv("PATH=:$astrologsrc");
//
//        if (strlen($info['h_sys']) != 1) $info['h_sys'] = "p";
//
//        exec ("astrolog -edir$astrolog -b{$info['utdatenow']} -ut{$info['utnow']} -p01 -eswe -house{$info['longitude']},{$info['latitude']},{$info['h_sys']} -flsj -g, -head", $out1);
//
//        exec ("astrolog -edir$astrolog -b{$info['utdatenow_brefore']} -ut{$info['utnow_brefore']} -p01 -eswe -house{$info['longitude_before_now']},{$info['latitude_before_now']},{$info['h_sys']} -flsj -g, -head", $out2);
//        exec ("astrolog -edir$astrolog -b{$info['utdatenow_after']} -ut{$info['utnow_after']} -p01 -eswe -house{$info['longitude_before_now']},{$info['latitude_before_now']},{$info['h_sys']} -flsj -g, -head", $out3);
//        if ($type == 'sun')
//        {
//            // 出生生日那一天太阳位置
//            $longtitude1 = sp($out1[0],',')[0] * 1;
//            // 当年生日前一天的太阳位置
//            $longtitude2 = sp($out2[0],',')[0] * 1;
//            // 当年生日后一天太阳位置
//            $longtitude3 = sp($out3[0],',')[0] * 1;
//        }
//        elseif ($type == 'moon')
//        {
//            // 那一天月亮位置
//            $longtitude1 = sp($out1[1],',')[0] * 1;
//            // 前一天的月亮位置
//            $longtitude2 = sp($out2[1],',')[0] * 1;
//            // 后一天月亮位置
//            $longtitude3 = sp($out3[1],',')[0] * 1;
//        }
//        $diff_longtitude = round(abs($longtitude3 - $longtitude2),6);//度数差
//
//        $diff_minutes = 24 * 60 * 60;//精确到秒
//
//        $diff_influence = $diff_longtitude / $diff_minutes;// 每秒钟走的度数
//
//        // 需要多少时间  回到需要的度数
//        $second = abs($longtitude1 - $longtitude2) / $diff_influence;// 总秒钟数
//
//        $hour = floor($second/3600);// 小时数
//
//        $minute = floor(($second - $hour * 3600 )/60);// 分钟数
//
//        $second = floor($second - $hour * 3600  - $minute * 60);// 秒钟数
//
//        $ymd = sp($info['utdatenow_brefore'],'.');
//
//        $hms = sp($info['utnow_brefore'],':');
//
//        if ($longtitude1 < $longtitude2)
//        {
//            $hms[0] -= $hour;
//            $hms[1] -= $minute;
//            $hms[2] -= $second;
//        }
//        else
//        {
//            $hms[0] += $hour;
//            $hms[1] += $minute;
//            $hms[2] += $second;
//        }
//        // 此处加上时区的时间
//        $date = mktime($hms[0], $hms[1], $hms[2], $ymd[1], $ymd[0], $ymd[2]);
//
//        $utdatenow = strftime("%d.%m.%Y", $date);
//        $utnow = strftime("%H:%M:%S", $date);
//
//        exec ("astrolog -edir$astrolog -b$utdatenow -ut$utnow -p01 -eswe -house{$info['longitude_before_now']},{$info['latitude_before_now']},{$info['h_sys']} -flsj -g, -head", $out4);
//
//        if($type == 'sun')
//        {
//            $longtitude3 = sp($out4[0],',')[0] * 1;
//        }
//        elseif($type == 'moon')
//        {
//            $longtitude3 = sp($out4[1],',')[0] * 1;
//        }
//
//        // 需要多少时间  回到需要的度数
//        $second = abs($longtitude1 - $longtitude3) / $diff_influence;// 总秒钟数
//
//        $hour = floor($second/3600);// 小时数
//
//        $minute = floor(($second - $hour * 3600 )/60);// 分钟数
//
//        $second = floor($second - $hour * 3600  - $minute * 60);// 秒钟数
//
//        if ($longtitude1 < $longtitude3)
//        {
//            $hms[0] -= $hour;
//            $hms[1] -= $minute;
//            $hms[2] -= $second;
//        }
//        else
//        {
//            $hms[0] += $hour;
//            $hms[1] += $minute;
//            $hms[2] += $second;
//        }
//        $date1 = mktime($hms[0] + 8, $hms[1], $hms[2], $ymd[1], $ymd[0], $ymd[2]);
//        // 此处加上时区的时间
//        $date = mktime($hms[0], $hms[1], $hms[2], $ymd[1], $ymd[0], $ymd[2]);
//
//        $utdatenow = strftime("%d.%m.%Y", $date);
//        $utnow = strftime("%H:%M:%S", $date);
//
//        // 加上时区后的时间
//        $this->returnTime = strftime("%Y-%m-%d", $date1) .' '. strftime("%H:%M", $date1);
//
//        return [$utdatenow, $utnow];
//    }
//    /**
//     * 获取次限盘的行星度数
//     * @param array $data1
//     * @param array $data2
//     * @param float $jd_to_use
//     */
//    public function getTheProgressionsLongtitude($data1 = array(), $data2 = array(), $jd_to_use = 0, $latitude = 0)
//    {
//        foreach($data2 as $k => $v) {
//            $longitude2[$k] = @sp($v,',')[0] * 1;
//            $speed[$k] = @sp($v,',')[1] * 1;
//        }
//
//        $p_mc = Crunch($data1['p1'][MC] + $longitude2[0] - $data1['p1'][0]);
//        $ob_deg = Get_OB_Ecl($jd_to_use);
//
//        $p_RAMC = r2d(atan(Cosine($ob_deg) * Sine($p_mc) / Cosine($p_mc)));
//
//        if (Cosine($p_mc) < 0)$p_RAMC = $p_RAMC + 180;
//        if ($p_RAMC < 0)$p_RAMC = $p_RAMC + 360;
//
//        $a1 = atan(Cosine($p_RAMC) / (-Sine($p_RAMC) * Cosine($ob_deg) - tan(d2r($latitude)) * Sine($ob_deg)));
//
//        if ($a1 < 0) $a1 = $a1 + pi();
//        if (Cosine($p_RAMC) < 0) $a1 = $a1 +  pi();
//
//        $longitude2[ASC] = Mod360(r2d($a1));
//        $longitude2[IC] = $p_mc > 180 ? $p_mc- 180 : $p_mc + 180;
//        $longitude2[DES] = $longitude2[ASC] > 180 ? $longitude2[ASC]- 180 : $longitude2[ASC] + 180;
//        $longitude2[MC] = $p_mc;
//        // 计算福点位置
//        if($this->day_chart == true) $longitude2[FORT] = $longitude2[FORT] + $longitude2[1] - $longitude2[0];
//        else $longitude2[FORT] = $longitude2[ASC] - $longitude2[1] + $longitude2[0];
//        if ($longitude2[FORT] >= 360) $longitude2[FORT] = $longitude2[FORT] - 360;
//        if ($longitude2[FORT] < 0) $longitude2[FORT] = $longitude2[FORT] + 360;
//
//
//        $hc = [];
//        // 此处 加入特殊分宫制   整宫制
//        if($this->h_sys == 'w')
//        {
//            $hc[1] = floor($longitude2[ASC]/30) *30;// 上升点
//            for ($i=1;$i <= 14; $i++)
//            {
//                $house = $hc[1] + ($i-1)*30;
//                if($house > 360) $house -=360;
//                $hc[$i] = $house;
//                unset($longitude2[14 + $i]);
//            }
//        }
//        else
//        {
//            for ($i = 1; $i <= 12; $i++)
//            {
//                $hc[$i] = Crunch($data1['hc1'][$i] + $longitude2[0] - $data1['p1'][0]);
//            }
//            $hc[1] = $longitude2[ASC];
//            $hc[4] = $longitude2[IC];
//            $hc[7] = $longitude2[DES];
//            $hc[10] = $longitude2[MC];
//        }
//
//        $retro = $this->getRetroData($speed);
//
//        return ['rx1'=>$retro,'p1'=>$longitude2,'hc1'=>$hc,'speed'=>$speed];
//    }

    /**
     * * 三限盘数据计算
     * @param array $before 上整月
     * @param array $after 下整月
     * @param int $changePro 变化比率
     * @return array
     */
    public function getProgressionThirdprogressed($before = array(), $after = array(), $changePro = 0)
    {
        $temp = [];
        foreach ($before as $k => $v)
        {
            $t1 = explode(',',$before[$k]);
            $t2 = explode(',',$after[$k]);
            $p1 = $t1[0] * 1;
            $p2 = $t2[0] * 1;

            if (abs($p1 - $p2) > 180)
            {
                $p = $p1 + ($p2 + 360 - $p1) * $changePro;
            }else{
                $p = $p1 + ($p2 - $p1) * $changePro;
            }
            if( $p>= 360) $p = $p - 360;
            elseif( $p < 0) $p = $p + 360;
            // TODO d/s   计算真实平均值
            $r1 = isset($t1[2]) ? $t1[2] * 1 : 0;
//            $r2 = isset($t2[2]) ? $t2[2] * 1 : 0;
//            $r = $r1 + ($r2 - $r1) * $changePro;

            $temp[] = $p . ','. $t1[1] .',' . $r1;
        }

        return $temp;
    }
    /**
     * 计算行星  1入庙 或 2失势
     * @param null $p_id 行星ID
     * @param null $p_insign_id 行星落座id
     * @param float $p_insign_deg 行星落座度数
     * @return array
     */
    public function planet_in_ancient($p_id = null,$p_insign_id = null, $p_insign_deg = 0.0)
    {
        // 非古典七星落座位置也有该位置庙旺弱陷界十度信息
        if ($p_id === null || $p_id < 0 || $p_insign_id === null) return null;
        $ret = [
            'power'=> 0,
            'walls' => -1,   'walls_cn' => '',// 庙
            'promote'=> -1,  'promote_cn'=> '',// 旺
            'trisection'=>-1,'trisection_cn'=>'',// 三分
            'extent'=>-1,    'extent_cn'=>'',// 界
            'period'=>-1,      'period_cn'=>'',// 面
            'migration' => -1, 'migration_cn'=>'',// 游走
            'or_walls_planet_id'=>-1,'or_promote_planet_id'=>-1,
            'or_extent_planet_id'=>-1,'or_period_planet_id'=>-1,
            'or_trisection_planet_id'=>[],
        ];

        $fall = ($p_insign_id <= 6) ? ($p_insign_id + 6) : ($p_insign_id - 6);

        $config_ancient = config('astrolog.ancient')[$p_insign_id];
        $config_ancient_fall = config('astrolog.ancient')[$fall];

        // 入庙
        if ($p_id < 7 && $config_ancient[0] == $p_id)
        {
            $ret['power'] += 5;
            $ret['walls']=1;
            $ret['walls_cn']='入庙';
        }
        elseif($p_id < 7 && $config_ancient_fall[0] == $p_id)
        {
            $ret['power'] -= 5;
            $ret['walls']=2;
            $ret['walls_cn']='失势';
        }

        $ret['or_walls_planet_id'] = $config_ancient[0];
        $ret['or_walls_fall_planet_id'] = $config_ancient_fall[0];
        // 天王星  入庙 失势
        if($p_id == 7)
        {
            if($p_insign_id == 11)
            {
                $ret['power'] += 5;
                $ret['walls']=1;
                $ret['walls_cn']='入庙';
            }
            elseif($p_insign_id == 5)
            {
                $ret['power'] -= 5;
                $ret['walls']=2;
                $ret['walls_cn']='失势';
            }
//            $ret['or_walls_planet_id'] = 11;
//            $ret['or_walls_fall_planet_id'] = 5;
        }
        // 海王星  入庙 失势
        if($p_id == 8)
        {
            if($p_insign_id == 12)
            {
                $ret['power'] += 5;
                $ret['walls']=1;
                $ret['walls_cn']='入庙';
            }
            elseif($p_insign_id == 6)
            {
                $ret['power'] -= 5;
                $ret['walls']=2;
                $ret['walls_cn']='失势';
            }
//            $ret['or_walls_planet_id'] = 12;
//            $ret['or_walls_fall_planet_id'] = 6;
        }
        // 冥王星  入庙 失势
        if($p_id == 9)
        {
            if($p_insign_id == 8)
            {
                $ret['power'] += 5;
                $ret['walls']=1;
                $ret['walls_cn']='入庙';
            }
            elseif($p_insign_id == 2)
            {
                $ret['power'] -= 5;
                $ret['walls']=2;
                $ret['walls_cn']='失势';
            }
//            $ret['or_walls_planet_id'] = 8;
//            $ret['or_walls_fall_planet_id'] = 2;
        }
        // 擢升
        if ($config_ancient[1] !== '' && $config_ancient[1] == $p_id )
        {
            $ret['power'] += 4;
            $ret['promote']=1;
            $ret['promote_cn']='擢升';
        }
        elseif($config_ancient_fall[1] !== '' && $config_ancient_fall[1] == $p_id)
        {
            $ret['power'] -= 4;
            $ret['promote']=2;
            $ret['promote_cn']='落陷';
        }
        // 当擢升没有时  保持原始值-1【int】
        $ret['or_promote_planet_id'] = $config_ancient[1] !== '' ? $config_ancient[1] : -1;
        $ret['or_promote_fall_planet_id'] = $config_ancient_fall[1] !== '' ? $config_ancient_fall[1] : -1;
        // 三分
        if (in_array($p_id, $config_ancient[2]))
        {
            $ret['power'] += 3;
            $ret['trisection']=1;
            $ret['trisection_cn']='三分';
        }
        $ret['or_trisection_planet_id'] = $config_ancient[2];
        // 界主 界主为1 +2
//        if (session('xzns.extent') == 'ptolemy_world')
//            $extent = $config_ancient[3]['ptolemy_world'];
//        else
        // 使用埃及界
        $extent = $config_ancient[3]['default'];

        for ($i = 0; $i < 5;$i++)
        {
            if ($p_insign_deg > $extent[1][$i] && $p_insign_deg < $extent[1][$i + 1])
            {
                if ($extent[0][$i] == $p_id)
                {
                    $ret['power'] += 2;
                    $ret['extent'] = 1;
                    $ret['extent_cn']='界主';
                }
                $ret['or_extent_planet_id'] = $extent[0][$i];
            }
        }
        // 旬主或十度
        for($i = 0; $i < 3;$i++)
        {
            $min = $i * 10;
            $max = $min + 10;
            if ($p_insign_deg > $min && $p_insign_deg < $max)
            {
                if ($config_ancient[4][$i] == $p_id)
                {
                    $ret['power'] += 1;
                    $ret['period'] = 1;
                    $ret['period_cn']='旬主';
                }
                $ret['or_period_planet_id'] = $config_ancient[4][$i];
            }
        }

        if($ret['walls'] == -1 && $ret['promote'] == -1 && $ret['trisection'] == -1 && $ret['extent'] == -1 && $ret['period'] == -1 && $ret['power'] == 0 && $p_id < 7)
        {
            $ret['power'] = -5;
            $ret['migration']=1;
            $ret['migration_cn']='游走';
        }
        if( $p_id > 6 && $p_id < 9 && $ret['walls'] == -1 )
        {
            $ret['power'] = -5;
            $ret['migration']=1;
            $ret['migration_cn']='游走';
        }
        // 所有星座的29°
        if($p_insign_deg >= 29) $ret['diversity'] = '岐度';
        else $ret['diversity'] = '';

        // 基本星座（白羊、巨蟹、天秤、摩羯）：0°，13°，26°
        // 固定星座（金牛、狮子、天蝎、水瓶）：9°，21°
        // 变动星座（双子、处女、射手、双鱼）：4°，17°
        if((in_array($p_insign_id,[1,4,7,10]) && in_array(floor($p_insign_deg),[0,13,26]))
            || (in_array($p_insign_id,[2,5,8,11]) && in_array(floor($p_insign_deg),[9,21]))
            || (in_array($p_insign_id,[3,6,9,12]) && in_array(floor($p_insign_deg),[4,17])))
            $ret['vital'] = '紧要度数';
        else $ret['vital'] = '';

        // 金牛座的29度、狮子座 6度、射手座 8度
        if(($p_insign_id == 2 && floor($p_insign_deg) == 29)
            || ($p_insign_id == 5 && floor($p_insign_deg) == 6)
            || ($p_insign_id == 9 && floor($p_insign_deg) == 8)
        )
            $ret['defect'] = '缺点度数';
        else $ret['defect'] = '';

        return $ret;
    }
    /**
     * 计算12个宫头  宫神星
     * @param int $insignid
     * @param int $insigndeg
     * @return array
     */
    public function house_almuten($insignid = 0, $insigndeg=0)
    {
        $score = [0,0,0,0,0,0,0];// 初始分数为0
        $data = config('astrolog.ancient')[$insignid];
        $score[$data[0]] += 5;// 本垣
        if($data[1] != '') $score[$data[1]] += 4;// 擢升
        $score[$data[2][0]] += 3;// 三分 此处取第一三分主星
        // 界主 界主为1 +2
//        if (session('xzns.extent') == 'ptolemy_world') $extent = $data[3]['ptolemy_world'];
//        else
            $extent = $data[3]['default'];

        for ($i = 0; $i < 5;$i++)
        {
            if ($insigndeg > $extent[1][$i] && $insigndeg < $extent[1][$i + 1])
                $score[$extent[0][$i]] += 2;// 界
            if($i < 3)
            {
                $min = $i * 10; $max = $min + 10;
                if ($insigndeg > $min && $insigndeg < $max)
                    $score[$data[4][$i]] += 1;// 面
            }
        }
        $pid = array_search(max($score),$score);
        $re = [
            'id'=>$pid,
        ];
        // 分值最高星为宫神星
        return $pid;
    }
    /**
     * 计算得时失时
     * @param null $pid
     * @param int $insignid
     * @param int $inhouseid
     */
    public function planet_hagy($pid=null,$insignid=0,$inhouseid=0,$sun_in_out = 0)
    {
        if($pid > 6) return -1;// 只计算日月水金火木土0123456
        $day_planet = [0,5,6];// 昼行星
        $night_planet = [1,3,4];// 夜行星
        if($pid == 2)
        {
            // 水星东出为昼行星  西入为夜行星
            if($sun_in_out == 0) $day_planet[] = 2;
            elseif($sun_in_out == 1) $night_planet[] = 2;
        }
        // 日生盘
        if($this->day_chart == true)
        {
            // 得时
            if(in_array($pid,$day_planet) && $insignid%2==1 && $inhouseid > 6) return 0;// 得时
            // 失时
            if(in_array($pid,$night_planet) && $insignid%2==1 && $inhouseid > 6) return 1;// 失时
        }
        // 夜生盘
        else
        {
            // 得时
            if(in_array($pid,$night_planet) && $insignid%2==0 && $inhouseid > 6) return 0;
            // 失时
            if(in_array($pid,$day_planet) && $insignid%2==0 && $inhouseid > 6) return 1;
        }
        return -1;
    }
    /**
     * 计算行星的东出西入
     * @param int $sunDeg
     * @param int $pDeg
     * @return string
     */
    public function oriental_or_occidental_sun($pid=null,$sunDeg=0,$pDeg=0)
    {
        if($pid > 6 || !$pid) return -1;
        if($sunDeg > 180)
        {
            $min = $sunDeg - 180;
            $max = $sunDeg;
            if($pDeg > $min && $pDeg < $max) return 0;// 东出
            if($pDeg < $min || $pDeg > $max) return 1;// 西入
        }
        else
        {
            $min = $sunDeg;
            $max = $sunDeg + 180;
            if($pDeg > $min && $pDeg < $max) return 1;// 西入
            if($pDeg < $min || $pDeg > $max) return 0;// 东出

        }
        return false;
    }
    /**
     * 计算行星是否在日光下  灼伤 日心
     * @param int $sunDeg
     * @param int $pDeg
     * @return string
     */
    public function planet_sun_dist($pid=null,$sunDeg=0,$pDeg=0)
    {
        if($pid > 6 || !$pid) return -1;
        if(abs($sunDeg - $pDeg) > 270)
        {
            if($sunDeg > $pDeg)
                $dist = $pDeg + 360 - $sunDeg;
            else
                $dist = $sunDeg + 360 - $pDeg;
        }else{
            $dist = abs($sunDeg - $pDeg);
        }
        if($dist > 17.5) return 0;// 无
        elseif($dist > 8.5) return 1;// 在日光下
        elseif($dist > 17/60) return 2;// 灼伤
        else return 3;// 日心 日核
    }
    /**
     * 判断行星 喜乐宫
     * @param null $pid
     * @param int $inhouseid
     * @return array
     */
    public function planet_in_like_house($pid = null,$inhouseid=0)
    {
        if($pid === null || !$inhouseid || $pid > 6) return [-1,-1];
        if     ($pid == 0)
            if($inhouseid == 9) return [1,9];
            else return [0,9];
        elseif ($pid == 1)
            if($inhouseid == 3) return [1,3];
            else return [0,3];
        elseif ($pid == 2)
            if($inhouseid == 1) return [1,1];
            else return [0,1];
        elseif ($pid == 3)
            if($inhouseid == 5) return [1,5];
            else return [0,5];
        elseif ($pid == 4)
            if($inhouseid == 6) return [1,6];
            else return [0,6];
        elseif ($pid == 5)
            if($inhouseid == 11) return [1,11];
            else return [0,11];
        elseif ($pid == 6)
            if($inhouseid == 12) return [1,12];
            else return [0,12];
    }
    /**
     * 判断行星是否位于燃烧之路
     * @param null $pid 行星id
     * @param int $insignid 行星落座id
     * @param int $insigndeg 行星落在当前星座多少度
     * @return bool|string
     */
    public function planet_in_burn_way($pid=null,$insignid=0,$insigndeg=0)
    {
        if($pid > 6 || $pid === null || !$insignid) return -1;
        if(($insignid == 7 && $insigndeg >= 15) || ($insignid == 8  && $insigndeg <= 15)) return 1;// 燃烧之路
        else return 0;
    }
    /**
     * 计算古典七星-行星映点antipode 与 反映点reflect
     * @param null $pid 行星
     * @param int $pDeg
     * @return array
     */
    public function planet_antipode($pid=null,$pDeg=0,$p = [])
    {
        $orb = 2;
        if($pid > 6 || $pid === null || empty($p)) return null;
        // 离冬至点即摩羯座0°近
        if(abs($pDeg - 90) > abs($pDeg - 270)) $axisDeg = 540;
        // 离夏至点即巨蟹座0°近
        else $axisDeg = 180;

        $antipode_deg = $axisDeg - $pDeg;
        $reflect_deg = $antipode_deg > 180 ? $antipode_deg - 180 : $antipode_deg + 180;

        $ret['antipode_deg'] = $antipode_deg;
        $ret['reflect_deg'] = $reflect_deg;
        $ret['antipode_planet'] = [];
        $ret['reflect_planet'] = [];

        foreach($p as $i => $d)
        {
            if($i > MC) continue;
            if(abs($antipode_deg - $d) <= $orb) $ret['antipode_planet'][] = $i;
            if(abs($reflect_deg - $d) <= $orb) $ret['reflect_planet'][] = $i;
        }

        return $ret;
    }
    // 计算互容接纳
    public function recept_or_mutual_recept($planets = [])
    {
        $ret = [];
        $miscible = [];// 互溶
        $accept = [];// 接纳
        // 古典七星
        for($i=0;$i <= 5;$i++)
        {
            for($l=0;$l<=6;$l++)
            {
                $i_ancient = $this->planet_in_ancient($i,$planets[$l]['in_sign_id'],$planets[$l]['in_sign_deg_e']);
                $l_ancient = $this->planet_in_ancient($l,$planets[$i]['in_sign_id'],$planets[$i]['in_sign_deg_e']);

                // 互容
                if($i_ancient['power'] > 3 && $l_ancient['power'] > 3 && $i < $l)
                {
                    $ancient1 = '';
                    if($i_ancient['walls']==1) $ancient1 .= '+本垣';
                    if($i_ancient['promote']==1) $ancient1 .= '+擢升';
                    if($i_ancient['trisection']==1) $ancient1 .= '+三分';
                    if($i_ancient['extent']==1) $ancient1 .= '+界主';
                    if($i_ancient['period']==1) $ancient1 .= '+十度';
                    $ancient1 = mb_substr($ancient1,1);
                    $ancient2 = '';
                    if($l_ancient['walls']==1) $ancient2 .= '+本垣';
                    if($l_ancient['promote']==1) $ancient2 .= '+擢升';
                    if($l_ancient['trisection']==1) $ancient2 .= '+三分';
                    if($l_ancient['extent']==1) $ancient2 .= '+界主';
                    if($l_ancient['period']==1) $ancient2 .= '+十度';
                    $ancient2 = mb_substr($ancient2,1);
                    if(isset($planets[$i]['phase'][$i.'-'.$l]) || isset($planets[$i]['phase'][$l.'-'.$i]))
                        $have_phase = true;
                    else
                        $have_phase = false;
                    $miscible[] = $i.'-'.$l;
                    $miscible[] = $l.'-'.$i;
                    $ret[2][] = [
                        'id1'=>$i,
                        'ancient1'=>$ancient2,
                        'id2'=>$l,
                        'ancient2'=>$ancient1,
                        'phase'=>$have_phase,// 是否成相位
                        'relation_by'=>[
                            'walls1'=>$i_ancient['walls'],
                            'promote1'=>$i_ancient['promote'],
                            'trisection1'=>$i_ancient['trisection'],
                            'extent1'=>$i_ancient['extent'],
                            'period1'=>$i_ancient['period'],
                            'walls2'=>$l_ancient['walls'],
                            'promote2'=>$l_ancient['promote'],
                            'trisection2'=>$l_ancient['trisection'],
                            'extent2'=>$l_ancient['extent'],
                            'period2'=>$l_ancient['period'],
                        ],
                        'relation'=>'互容',
                        'status'=>2,
                    ];
                }
                elseif(isset($planets[$i]['phase'][$i.'-'.$l]) && $l_ancient['power'] > 3)
                {
                    // 没有互溶 和 接纳
                    if(!in_array($i.'-'.$l,$miscible) && !in_array($i.'-'.$l,$accept))
                    {
                        // 存在相位  接纳
                        $ancient2 = '';
                        $ancient1 = '';
                        if($l_ancient['walls']==1) $ancient2 .= '+本垣';
                        if($l_ancient['promote']==1) $ancient2 .= '+擢升';
                        if($l_ancient['trisection']==1) $ancient2 .= '+三分';
                        if($l_ancient['extent']==1) $ancient2 .= '+界主';
                        if($l_ancient['period']==1) $ancient2 .= '+十度';
                        $ancient2 = mb_substr($ancient2,1);

                        $accept[] = $i.'-'.$l;
                        $ret[1][] = [
                            'id1'=>$i,
                            'ancient1'=>'',
                            'id2'=>$l,
                            'ancient2'=>$ancient2,
                            'relation_by'=>[
                                'walls'=>$l_ancient['walls'],'promote'=>$l_ancient['promote'],'trisection'=>$l_ancient['trisection'],
                                'extent'=>$l_ancient['extent'],'period'=>$l_ancient['period']
                            ],
                            'relation'=>'接纳',
                            'status'=>1,
                        ];
                    }
                }
                elseif(isset($planets[$l]['phase'][$l.'-'.$i]) && $i_ancient['power'] > 3)
                {
                    // 没有互容 和 接纳
                    if(!in_array($l.'-'.$i,$miscible) && !in_array($l.'-'.$i,$accept))
                    {
                        // 存在相位  接纳
                        $ancient2 = '';
                        $ancient1 = '';
                        if($i_ancient['walls']==1) $ancient1 .= '+本垣';
                        if($i_ancient['promote']==1) $ancient1 .= '+擢升';
                        if($i_ancient['trisection']==1) $ancient1 .= '+三分';
                        if($i_ancient['extent']==1) $ancient1 .= '+界主';
                        if($i_ancient['period']==1) $ancient1 .= '+十度';
                        $ancient1 = mb_substr($ancient1,1);
                        $accept[] = $l.'-'.$i;
                        $ret[1][] = [
                            'id1'=>$l,
                            'ancient1'=>'',
                            'id2'=>$i,
                            'ancient2'=>$ancient1,
                            'relation_by'=>[
                                'walls'=>$i_ancient['walls'],'promote'=>$i_ancient['promote'],'trisection'=>$i_ancient['trisection'],
                                'extent'=>$i_ancient['extent'],'period'=>$i_ancient['period']
                            ],
                            'relation'=>'接纳',
                            'status'=>1,
                        ];
                    }
                }
            }
        }
        // 古典七星
        for($i=0;$i <= 6;$i++)
        {
            // 所有星座的29° 岐度
            if($planets[$i]['ancient']['diversity'])
            {
                $ret[3][] = [
                    'id1'=>$i,
                    'id2'=>-1,
                    'event'=>'位于岐度',
                    'status'=>3,
                ];
            }
            // 紧要度数
            if($planets[$i]['ancient']['vital'])
            {
                $ret[4][] = [
                    'id1'=>$i,
                    'id2'=>-1,
                    'event'=>'位于紧要度数',
                    'status'=>4,
                ];
            }
            // 缺点度数
            if($planets[$i]['ancient']['defect'])
            {
                $ret[5][] = [
                    'id1'=>$i,
                    'id2'=>-1,
                    'event'=>'位于缺点度数',
                    'status'=>5,
                ];
            }
            // 映点
            if(!empty($planets[$i]['ancient']['antipode']['antipode_planet']) && $i < $planets[$i]['ancient']['antipode']['antipode_planet'][0])
            {
                $ret[6][] = [
                    'id1'=>$i,
                    'id2'=>$planets[$i]['ancient']['antipode']['antipode_planet'][0],
                    'event'=>'成映点',
                    'status'=>6,
                ];
            }
            // 成反映点
            if(!empty($planets[$i]['ancient']['antipode']['reflect_planet']) && $i < $planets[$i]['ancient']['antipode']['reflect_planet'][0])
            {
                $ret[7][] = [
                    'id1'=>$i,
                    'id2'=>$planets[$i]['ancient']['antipode']['reflect_planet'][0],
                    'event'=>'成反映点',
                    'status'=>7,
                ];
            }
            // 燃烧之路
            if($planets[$i]['ancient']['burn_way'])
            {
                $ret[8][] = [
                    'id1'=>$i,
                    'id2'=>-1,
                    'event'=>'位于燃烧之路',
                    'status'=>8,
                ];
            }
        }
        return $ret;
    }
    /**
     * 计算阿拉伯点
     * @param array $planets
     * @param bool $day_night  日生盘true或夜生盘 false
     * @return array
     */
    public function getTheArabics($planets = [],$houses = [],$day = true)
    {
        $ret = [];
        if($day)
        {
            $fortune = $houses[1]['deg'] + $planets[1]['deg'] - $planets[0]['deg'];
            $spirit = $houses[1]['deg'] + $planets[0]['deg'] - $planets[1]['deg'];
            $children = $planets[6]['deg'] - $planets[5]['deg'] + $houses[1]['deg'];
            $knowledge = $houses[1]['deg'] + $planets[1]['deg'] - $planets[2]['deg'];
        }else{
            $fortune = $houses[1]['deg'] + $planets[0]['deg'] - $planets[1]['deg'];
            $spirit = $houses[1]['deg'] + $planets[1]['deg'] - $planets[0]['deg'];
            $children = $planets[5]['deg'] - $planets[6]['deg'] + $houses[1]['deg'];
            $knowledge = $houses[1]['deg'] + $planets[2]['deg'] - $planets[1]['deg'];
        }
        $marriage = $houses[1]['deg'] + $houses[7]['deg'] - $planets[3]['deg'];
        if($fortune >= 360) $fortune -= 360;
        elseif($fortune < 0) $fortune += 360;
        if($spirit >= 360) $spirit -= 360;
        elseif($spirit < 0) $spirit += 360;
        if($children >= 360) $children -= 360;
        elseif($children < 0) $children += 360;
        if($knowledge >= 360) $knowledge -= 360;
        elseif($knowledge < 0) $knowledge += 360;
        if($marriage >= 360) $marriage -= 360;
        elseif($marriage < 0) $marriage += 360;

        $ret = [
            0=>[
                'name'=>'福点',
                'deg'=>$fortune,
                'in_sign_deg'=>$this->inSignDeg($fortune),
                'in_sign_id'=>ceil($fortune/30),
            ],
            1=>[
                'name'=>'精神点',
                'deg'=>$spirit,
                'in_sign_deg'=>$this->inSignDeg($spirit),
                'in_sign_id'=>ceil($spirit/30),
            ],
            2=>[
                'name'=>'子女点',
                'deg'=>$children,
                'in_sign_deg'=>$this->inSignDeg($children),
                'in_sign_id'=>ceil($children/30),
            ],
            3=>[
                'name'=>'知识点',
                'deg'=>$knowledge,
                'in_sign_deg'=>$this->inSignDeg($knowledge),
                'in_sign_id'=>ceil($knowledge/30),
            ],
            4=>[
                'name'=>'婚姻点',
                'deg'=>$marriage,
                'in_sign_deg'=>$this->inSignDeg($marriage),
                'in_sign_id'=>ceil($marriage/30),
            ],
        ];
        return $ret;
    }
    /**
     * 相位出入相
     * @param $asp int 相位度数
     * @param $deg1 float 行星度数1
     * @param $deg2 float 行星度数2
     * @param $speed1  float 速度1
     * @param $speed2 float 速度2
     * @param $vs 1=>行星1为本命 2=> 行星2为本命     本命盘速度为0
     * @return bool true 为入相  false 为出相
     */
    public function phase_in_or_out($asp,$deg1,$deg2,$speed1,$speed2,$vs=0)
    {
        $da = abs($deg1 - $deg2);

        if ($da > 180) $da = 360 - $da;
        // VS 本命盘计算出入相位
        if($vs)
        {
            if( $vs == 1 )
            {
                $fixed = $deg1;
                $speed1 = 0;
            }
            elseif( $vs == 2 )
            {
                $fixed = $deg2;
                $speed2 = 0;
            }
            $accurate_deg1 = $fixed + $asp;
            $accurate_deg2 = $fixed - $asp;

        }
        if(abs($speed1) > abs($speed2))
        {
            $s3 = $speed1 - $speed2;
            $da3 = abs($deg1 - $deg2+$s3);
        }
        else
        {
            $s3 = $speed2 - $speed1;
            $da3 = abs($deg1 - $deg2-$s3);
        }
        if($da3 > 180) $da3 = 360 - $da3;
        if(abs($da-$asp) > abs($da3-$asp)) return true;
        return false;

    }
    /**
     * 获取行星结构
     * @param array $base_data
     * @return array
     */
    public function get_structure($base_data = [])
    {
        if(empty($base_data)) return [];
        $base_data['structure'] = $this->aspect_patterns($base_data);
        $base_data['structure']['asterism'] = (object)$this->asterism($base_data);
        $base_data['structure']['midpoint'] = $this->planet_midpoint($base_data);
        $base_data['structure']['finger'] = $this->god_finger($base_data);
        return $base_data;
    }
    /**
     * 大三角
     * 120°-120°-120°
     * 小三角 与两颗星都成60度的这颗星称为顶星
     * 120°-60°-60°
     * T三角 与两星成90度的星体称为“T顶点”
     * 180°-90°-90°
     * 桥三角：两颗星180度，同时有第三颗星与两星分别成60度和120度。该星称为桥星。
     * 180°-60°-120°
     * 大十字：四颗星组成一个正四方形，每颗星都与另外两颗成90度（可以认为是在T三角基础上，加上一颗与T顶点对冲的行星）
     * 180°-90°-90°-180°
     * 大信封：桥三角的桥星对面多出一颗对冲的行星
     * 大风筝：大三角顶星的对面，多出一颗与它成180度的行星
     * @param array $base_data
     * @return array
     */
    protected function aspect_patterns($base_data = [])
    {
        $ret = [
            'trine'=>[],
            'small_triangle'=>[],
            'kite'=>[],
            't_square'=>[],
            'cross'=>[],
            'bridge_triangle'=>[],
            'envelop'=>[],
        ];
        for($i = 0; $i < 10; $i ++)
        {
            $a = $base_data['planets'][$i];
            $phasei = $a['phase'];
            for($j = 0; $j < 10; $j ++)
            {
                $b = $base_data['planets'][$j];
                $phasej = $b['phase'];
                // 大三角 + 小三角
                if (isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 120 && $i < $j)
                {
                    for($z = 0; $z < 10; $z ++)
                    {
                        $c = $base_data['planets'][$z];
                        // 大三角
                        if (isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 120 && $j < $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 120 )
                        {
                            $ret['trine'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                ],
                            ];
                            for ($y=0; $y < 10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];
                                // 大风筝：大三角顶星的对面，多出一颗与它成180度的行星
                                if (isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180)
                                {
                                    $ret['kite'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                            'top'=>true,
                                        ],
                                    ];
                                }
                            }
                        }
                        // 小三角
                        elseif(isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 60 && $j < $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 60 )
                        {
                            $ret['small_triangle'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ]
                            ];
                        }

                    }
                }
                // T三角 + 大十字
                elseif(isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 180 && $i < $j )
                {
                    for($z = 0; $z < 10; $z ++) {
                        $c = $base_data['planets'][$z];
                        $phasez = $c['phase'];
                        // T三角
                        if (isset($phasei[$i . "-" . $z]) && $phasei[$i . "-" . $z]['phase_e'] == 90 && $i != $z
                            && isset($phasej[$j . "-" . $z]) && $phasej[$j . "-" . $z]['phase_e'] == 90 && $j != $z
                        ) {
                            $ret['t_square'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ]
                            ];
                            for ($y=0; $y < 10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];

                                // 大十字
                                if(isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180 && $i < $z
                                    && isset($phasey[$y . "-" . $j]) && $phasey[$y . "-" . $j]['phase_e'] == 90
                                    && isset($phasey[$y . "-" . $i]) && $phasey[$y . "-" . $i]['phase_e'] == 90 && $z < $y
                                )
                                {
                                    $ret['cross'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                            'top'=>false,
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                            'top'=>true,
                                        ],
                                    ];
                                }
                            }
                        }
                        // 桥三角 两颗星180度，同时有第三颗星与两星分别成60度和120度。该星称为桥星。
                        // 大信封：桥三角的桥星对面多出一颗对冲的行星
                        elseif ($i != $z && $j != $z &&
                            ((isset($phasez[$z . "-" . $i]) && $phasez[$z . "-" . $i]['phase_e'] == 120
                                    && isset($phasez[$z . "-" . $j]) && $phasez[$z . "-" . $j]['phase_e'] == 60)
                                || (isset($phasez[$z . "-" . $i]) && $phasez[$z . "-" . $i]['phase_e'] == 60
                                    && isset($phasez[$z . "-" . $j]) && $phasez[$z . "-" . $j]['phase_e'] == 120))
                        )
                        {
                            $ret['bridge_triangle'][] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$z,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ],
                            ];
                            for ($y=0;$y<10;$y++)
                            {
                                $d = $base_data['planets'][$y];
                                $phasey = $d['phase'];
                                // 大信封
                                if (isset($phasey[$y . "-" . $z]) && $phasey[$y . "-" . $z]['phase_e'] == 180
                                    && $z < $y && $i< $z
                                )
                                {
                                    $ret['envelop'][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                        ],
                                        [
                                            'id'=>$z,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                        ],
                                        [
                                            'id'=>$y,
                                            'in_sign_id'=>$d['in_sign_id'],
                                            'in_house_id'=>$d['in_house_id'],
                                        ],
                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }
        return $ret;
    }
    /**
     * 星群：三颗以上行星聚拢，称作星群（不计算小行星）
     * 紧密星群：三颗以上行星，彼此合相
     * 疏松星群：三颗以上行星，不满足紧密星群条件，但每颗星都有一个合相
     * 分散星群：三颗以上行星，不满足前两种，但位于同一星座或同一宫位
     * @param array $base_data
     * @return array
     */
    protected function asterism($base_data = [])
    {
        $ret = [];
        $signs_arr = [];
        $houses_arr = [];
        for($i=0;$i<10;$i++)
        {
            $signs_arr[$base_data['planets'][$i]['in_sign_id']][] = $i;
            $houses_arr[$base_data['planets'][$i]['in_house_id']][] = $i;
        }
        $check = [];
        for($i = 0; $i < 10; $i ++)
        {
            $a = $base_data['planets'][$i];

            $phasei = $a['phase'];
            for($j = 0; $j < 10; $j ++)
            {
                $b = $base_data['planets'][$j];
                $phasej = $b['phase'];
                if(isset($phasei[$i . "-" . $j]) && $phasei[$i . "-" . $j]['phase_e'] == 0 && $i < $j)
                {
                    for($k=0;$k<10;$k++)
                    {
                        $c = $base_data['planets'][$k];
                        $phasek = $c['phase'];
                        // 紧密星群：三颗以上行星，彼此合相
                        if(isset($phasei[$i . "-" . $k]) && $phasei[$i . "-" . $k]['phase_e'] == 0 && $j < $k
                            && isset($phasej[$j . "-" . $k]) && $phasej[$j . "-" . $k]['phase_e'] == 0
                        )
                        {
                            $check_str = $i.'-'.$j.'-'.$k;
                            if(!$this->check_in($check_str,$check))
                            {
                                $check[] = $check_str;
                                $ret[1][] = [
                                    [
                                        'id'=>$i,
                                        'in_sign_id'=>$a['in_sign_id'],
                                        'in_house_id'=>$a['in_house_id'],
                                    ],
                                    [
                                        'id'=>$j,
                                        'in_sign_id'=>$b['in_sign_id'],
                                        'in_house_id'=>$b['in_house_id'],
                                    ],
                                    [
                                        'id'=>$k,
                                        'in_sign_id'=>$c['in_sign_id'],
                                        'in_house_id'=>$c['in_house_id'],
                                    ],
                                ];
                            }

                        }
                        // 疏松星群：三颗以上行星，不满足紧密星群条件，但每颗星都有一个合相
                        elseif($j < $k &&
                            ((isset($phasei[$i . "-" . $k]) && $phasei[$i . "-" . $k]['phase_e'] == 0)
                                || (isset($phasej[$j . "-" . $k]) && $phasej[$j . "-" . $k]['phase_e'] == 0))
                        )
                        {
                            $ss = 0;
                            for($g = 0;$g<10;$g++)
                            {
                                $d = $base_data['planets'][$g];
                                if($k<$g &&
                                    ((isset($phasei[$i . "-" . $g]) && $phasei[$i . "-" . $g]['phase_e'] == 0)
                                        || (isset($phasej[$j . "-" . $g]) && $phasej[$j . "-" . $g]['phase_e'] == 0)
                                        || (isset($phasek[$k . "-" . $g]) && $phasek[$k . "-" . $g]['phase_e'] == 0))
                                )
                                {
                                    $check_str = $i.'-'.$j.'-'.$k.'-'.$g;
                                    if(!$this->check_in($check_str,$check))
                                    {
                                        $check[] = $check_str;
                                        $ss = 1;
                                        $ret[2][] = [
                                            [
                                                'id'=>$i,
                                                'in_sign_id'=>$a['in_sign_id'],
                                                'in_house_id'=>$a['in_house_id'],
                                            ],
                                            [
                                                'id'=>$j,
                                                'in_sign_id'=>$b['in_sign_id'],
                                                'in_house_id'=>$b['in_house_id'],
                                            ],
                                            [
                                                'id'=>$k,
                                                'in_sign_id'=>$c['in_sign_id'],
                                                'in_house_id'=>$c['in_house_id'],
                                            ],
                                            [
                                                'id'=>$g,
                                                'in_sign_id'=>$d['in_sign_id'],
                                                'in_house_id'=>$d['in_house_id'],
                                            ],
                                        ];
                                    }
                                }
                            }
                            if($ss == 0)
                            {
                                $check_str = $i.'-'.$j.'-'.$k;
                                if(!$this->check_in($check_str,$check)) {
                                    $check[] = $check_str;
                                    $ret[2][] = [
                                        [
                                            'id'=>$i,
                                            'in_sign_id'=>$a['in_sign_id'],
                                            'in_house_id'=>$a['in_house_id'],
                                        ],
                                        [
                                            'id'=>$j,
                                            'in_sign_id'=>$b['in_sign_id'],
                                            'in_house_id'=>$b['in_house_id'],
                                        ],
                                        [
                                            'id'=>$k,
                                            'in_sign_id'=>$c['in_sign_id'],
                                            'in_house_id'=>$c['in_house_id'],
                                        ],
                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }
        // 分散星群 ：三颗或以上行星，不满足前两种，但位于同一星座或同一宫位
        for($i_i=1;$i_i<13;$i_i++)
        {
            $c_s = (isset($signs_arr[$i_i]) && count($signs_arr[$i_i])) ? count($signs_arr[$i_i]) : 0;
            $c_h = (isset($houses_arr[$i_i]) && count($houses_arr[$i_i])) ? count($houses_arr[$i_i]) : 0;
            // 位于同一星座
            if($c_s >= 3)
            {
                $a = $base_data['planets'][$signs_arr[$i_i][0]];
                $b = $base_data['planets'][$signs_arr[$i_i][1]];
                $c = $base_data['planets'][$signs_arr[$i_i][2]];
                $data = [
                    [
                        'id'=>$signs_arr[$i_i][0],
                        'in_sign_id'=>$a['in_sign_id'],
                        'in_house_id'=>$a['in_house_id'],
                    ],
                    [
                        'id'=>$signs_arr[$i_i][1],
                        'in_sign_id'=>$b['in_sign_id'],
                        'in_house_id'=>$b['in_house_id'],
                    ],
                    [
                        'id'=>$signs_arr[$i_i][2],
                        'in_sign_id'=>$c['in_sign_id'],
                        'in_house_id'=>$c['in_house_id'],
                    ]
                ];
                $check_str = $signs_arr[$i_i][0].'-'.$signs_arr[$i_i][1].'-'.$signs_arr[$i_i][2];
                if($c_s > 3)
                {
                    for($s=3;$s<$c_s;$s++)
                    {
                        $check_str .= '-'.$signs_arr[$i_i][$s];
                        $temp = $base_data['planets'][$signs_arr[$i_i][$s]];
                        $data[] = [
                            'id'=>$signs_arr[$i_i][$s],
                            'in_sign_id'=>$temp['in_sign_id'],
                            'in_house_id'=>$temp['in_house_id'],
                        ];
                    }
                }
                if(!$this->check_in($check_str,$check))
                {
                    $data = $this->reset_sort($check_str,$data,'in_house_id',$base_data);
                    // 同一星座的  按照宫位id排序
                    $check[] = $check_str;
                    $ret[3][] = $data;
                    unset($signs_arr[$i_i]);
                }
            }
            // 位于同一宫位
            if($c_h >= 3)
            {
                $a = $base_data['planets'][$houses_arr[$i_i][0]];
                $b = $base_data['planets'][$houses_arr[$i_i][1]];
                $c = $base_data['planets'][$houses_arr[$i_i][2]];
                $data = [
                    [
                        'id'=>$houses_arr[$i_i][0],
                        'in_sign_id'=>$a['in_sign_id'],
                        'in_house_id'=>$a['in_house_id'],
                    ],
                    [
                        'id'=>$houses_arr[$i_i][1],
                        'in_sign_id'=>$b['in_sign_id'],
                        'in_house_id'=>$b['in_house_id'],
                    ],
                    [
                        'id'=>$houses_arr[$i_i][2],
                        'in_sign_id'=>$c['in_sign_id'],
                        'in_house_id'=>$c['in_house_id'],
                    ]
                ];
                $check_str = $houses_arr[$i_i][0].'-'.$houses_arr[$i_i][1].'-'.$houses_arr[$i_i][2];
                if($c_h > 3)
                {
                    for($s=3;$s<$c_h;$s++)
                    {

                        $check_str .= '-'.$houses_arr[$i_i][$s];
                        $temp = $base_data['planets'][$houses_arr[$i_i][$s]];
                        $data[] = [
                            'id'=>$houses_arr[$i_i][$s],
                            'in_sign_id'=>$temp['in_sign_id'],
                            'in_house_id'=>$temp['in_house_id'],
                        ];
                    }
                }
                if(!$this->check_in($check_str,$check))
                {
                    $data = $this->reset_sort($check_str,$data,'in_sign_id',$base_data);
                    // 同一宫位的  按照星座id排序
                    unset($houses_arr[$i_i]);
                    $check[] = $check_str;
                    $ret[3][] = $data;

                }
            }
        }

        return $ret;
    }
    /**
     * 计算行星中点(包括四轴)
     * @param array $base_data
     * @return array
     */
    protected function planet_midpoint($base_data = [])
    {
        $ret = [];
        for($i=0;$i<10;$i++)
        {
            $a = $base_data['planets'][$i];

            $deg1 = $a['deg'];
            for ($j=$i+1;$j<10;$j++)
            {
                $b = $base_data['planets'][$j];
                $deg2 = $b['deg'];
                $mid = ($deg1 + $deg2) /2 ;// 中点度数 (A+B)/2
                for($k=0;$k<10;$k++)
                {
                    $c = $base_data['planets'][$k];
                    $deg3 = $c['deg'];
                    $diff = abs($deg3 - $mid); // 计算相差度数
                    if($diff > 180) $diff = 360 - $diff;
                    // 判断是否与行星形成 0°/90°/180°相位
                    if($i != $k && $j != $k
                        && (($diff <= 1 ) || ($diff > 89 && $diff < 91) || ($diff > 179 && $diff < 181) ))
                    {
                        $ret[] = [
                            'id1'=>$i,
                            'in_sign_id1'=>$a['in_sign_id'],
                            'in_house_id1'=>$a['in_house_id'],
                            'id2'=>$j,
                            'in_sign_id2'=>$b['in_sign_id'],
                            'in_house_id2'=>$b['in_house_id'],
                            'id3'=>$k,
                            'in_sign_id3'=>$c['in_sign_id'],
                            'in_house_id3'=>$c['in_house_id'],
                            'asp'=>$diff,
                        ];
                    }
                }
                $asc = $base_data['houses'][1];
                $mc = $base_data['houses'][10];
                $asc_deg = $asc['deg'];
                $mc_deg = $mc['deg'];
                $axis = [17=>$asc_deg,20=>$mc_deg];
                foreach($axis as $ak => $ax)
                {
                    $diff_axis = abs($ax - $mid); // 计算相差度数
                    if($diff_axis > 180) $diff_axis = 360 - $diff_axis;
                    if(($diff_axis <= 1 ) || ($diff_axis > 89 && $diff_axis < 91) || ($diff_axis > 179 && $diff_axis < 181) )
                    {
                        if($ak==17)
                        {
                            $name3 = '上升';
                            $insign = $asc['in_sign_id'];
                            $inhouse = 1;
//                        }
//                        if($ak==18)
//                        {
//                            $name3 = '天底';
//                            $insign = $ic['in_sign_id'];
//                            $inhouse = 4;
//                        }
//                        if($ak==19)
//                        {
//                            $name3 = '下降';
//                            $insign = $des['in_sign_id'];
//                            $inhouse = 7;
                        }
                        if($ak==20)
                        {
//                            $name3 = '天顶';
                            $insign = $mc['in_sign_id'];
                            $inhouse = 10;
                        }
                        $ret[] = [
                            'id1'=>$i,
                            'in_sign_id1'=>$a['in_sign_id'],
                            'in_house_id1'=>$a['in_house_id'],
                            'id2'=>$j,
                            'in_sign_id2'=>$b['in_sign_id'],
                            'in_house_id2'=>$b['in_house_id'],
                            'id3'=>$ak,
                            'in_sign_id3'=>$insign,
                            'in_house_id3'=>$inhouse,
                            'asp'=>$diff_axis,
//                            'output'=>$a['name_whole'].' + '.$b['name_whole'].' = '.$name3,
                        ];
                    }
                }
            }
        }
        return $ret;
    }
    /**
     * 上帝手指
     * 150°-150°-60°
     * 2-2-4
     */
    protected function god_finger($base_data = [])
    {
        $ret = [];
        for($i=0;$i<10;$i++)
        {
            $a = $base_data['planets'][$i];
            $deg1 = $a['deg'];
            for ($j = $i + 1; $j < 10; $j++)
            {
                $b = $base_data['planets'][$j];
                $deg2 = $b['deg'];
                $diff1_2 = abs($deg1 - $deg2); // 计算相差度数1-2
                if($diff1_2 > 180) $diff1_2 = 360 - $diff1_2;

                if($diff1_2 > 148 && $diff1_2 < 152)
                {
                    for ($k = 0; $k < 10; $k++)
                    {
                        $c = $base_data['planets'][$k];
                        $deg3 = $c['deg'];
                        $diff1_3 = abs($deg1 - $deg3); // 计算相差度数1-3
                        if($diff1_3 > 180) $diff1_3 = 360 - $diff1_3;
                        $diff2_3 = abs($deg2 - $deg3); // 计算相差度数2-3
                        if($diff2_3 > 180) $diff2_3 = 360 - $diff2_3;
                        if($i != $k && $j != $k
                            && (($diff1_3 > 148 && $diff1_3 < 152 && $diff2_3 > 56 && $diff2_3 < 64)
                                || ($diff1_3 > 56 && $diff1_3 < 64 && $diff2_3 > 148 && $diff2_3 < 152))
                        )
                        {
                            $ret[] = [
                                [
                                    'id'=>$i,
                                    'in_sign_id'=>$a['in_sign_id'],
                                    'in_house_id'=>$a['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$j,
                                    'in_sign_id'=>$b['in_sign_id'],
                                    'in_house_id'=>$b['in_house_id'],
                                    'top'=>false,
                                ],
                                [
                                    'id'=>$k,
                                    'in_sign_id'=>$c['in_sign_id'],
                                    'in_house_id'=>$c['in_house_id'],
                                    'top'=>true,
                                ],
                            ];
                        }

                    }
                }

            }
        }
        return $ret;
    }
    /**
     * 检查是否包含
     * @param string $string 如 0-2-6
     * @param array $arr 如 ['0-2-6-9','0-2-3-6']
     * @return bool
     */
    protected function check_in($string = '',$arr = [])
    {
        if(empty($arr)) return false;

        if(in_array($string,$arr)) return true;
        // 将字符串拆分成数组
//        $string_arr = explode('-',$string);
//        foreach ($arr as $a)
//        {
//            $num = 0;
//            $arr_arr = explode('-',$a);
//            foreach ($string_arr as $s)
//            {
//                if(in_array($s,$arr_arr)) $num++;
//            }
//            // 若包含  则立即返回true
//            if(count($string_arr) == $num) return true;
//        }
        return false;
    }
    /**
     * 重新排序
     * @param string $str
     * @param array $data
     * @param string $sort_by
     * @param array $base_data
     * @return array
     */
    protected function reset_sort($str = '',$data = [],$sort_by = 'deg',$base_data = [])
    {
        if($str == '') return [];
        foreach ($data as $k1 => $d1)
        {
            for($k2 = $k1+1;$k2<count($data);$k2++)
            {
                if($base_data['planets'][$data[$k1]['id']][$sort_by] > $base_data['planets'][$data[$k2]['id']][$sort_by])
                {
                    $temp = $data[$k1];// 此处不能用$d1
                    $data[$k1] = $data[$k2];
                    $data[$k2] = $temp;
                }
            }
        }
        return $data;
    }
}
